package com.aiti.lulian.controller.crad;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardZfFloorTextDto;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.crad.cluster.CardZfFloorText;
import com.aiti.lulian.service.ICardZfFloorTextService;
import com.aiti.lulian.service.ILuLianEnterpriseUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/zf_floor")
public class CardZfFloorTextController extends WebController {

    private final ICardZfFloorTextService baseService;

    private ILuLianEnterpriseUserService enterpriseUserService;

    public CardZfFloorTextController(ICardZfFloorTextService baseService,ILuLianEnterpriseUserService enterpriseUserService) {
        this.baseService = baseService;
        this.enterpriseUserService = enterpriseUserService;
    }


    @GetMapping("/selectZfFloorListByUserId")
    public Message selectZfFloorListByUserId(@RequestParam(value = "enterprise",required = false) String enterpriseId,
                                             @RequestParam(value = "flag",required = true) String flag){
        String eId = enterpriseId;
        if(StringUtils.isBlank(enterpriseId)){
            String userId = findUserId();
            LuLianEnterprise luLianEnterprise = enterpriseUserService.selectCurrentUserJoinEnterpriseInfo(userId);

            if(luLianEnterprise == null || StringUtils.isBlank(luLianEnterprise.getBaseId())){
                return Message.fail("没有找到绑定的企业信息");
            }
            eId = luLianEnterprise.getBaseId();
        }
        List<CardZfFloorText> result = baseService.selectZfFloorListByUserId(eId,flag);
        return Message.success(result);
    }

    @PostMapping("/deleteZfFloorText")
    public Message deleteZfFloorText(@RequestBody CardZfFloorText zfFloorText){
        baseService.deleteZfFloorText(zfFloorText);
        return Message.success();
    }

    /**
     * 新增或更新政府楼层
     */
    @ApiOperation("新增或更新政府楼层")
    @PostMapping("/saveOrUpdate")
    public Message saveOrUpdateCardZfFloorText(@RequestBody CardZfFloorTextDto cardZfFloorTextDto) {
        try {
            LuLianEnterprise luLianEnterprise = enterpriseUserService.selectCurrentUserJoinEnterpriseInfo(findUserId());

            if(luLianEnterprise == null || StringUtils.isBlank(luLianEnterprise.getBaseId())){
                return Message.fail("没有找到绑定的企业信息");
            }
            // 新增或更新
            return baseService.saveOrUpdateCardZfFloorText(cardZfFloorTextDto,luLianEnterprise.getBaseId());
        } catch (Exception e) {
            logger.error("新增或更新异常：===》" + e);
            return Error(e.getMessage());
        }
    }
}